package com.shell.governor.security.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.framework.utils.VerifyCodeUtil;
import com.shell.governor.security.user.dao.PubReservationInfoDao;
import com.shell.governor.security.user.po.PubReservationInfo;
import com.shell.governor.security.user.service.PubReservationInfoService;
import com.shell.governor.security.user.util.SecurityCodeUtil;
import com.shell.governor.user.dao.PubUserDao;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.user.vo.UserQueryVo;

@Service
public class PubReservationInfoServiceImpl extends PersistentServiceImpl<PubReservationInfo>
		implements PubReservationInfoService {
	@Autowired
	private PubUserDao pubUserDao;
	@Autowired
	private PubReservationInfoDao pubReservationInfoDao;

	public RetMsg selectPubReservationInfoByLoginName(String loginName, String infoDesc, String svertificationCode) {
		RetMsg msg = VerifyCodeUtil.checkVerifyCode(svertificationCode);
		if ("success" != msg.getType()) {
			return msg;
		}
		UserQueryVo user = new UserQueryVo();
		user.setLoginName(loginName);
		String[] notState = { "D" };
		user.setNotStates(notState);
		List<PubUserVo> users = this.pubUserDao.queryUser(user);
		if ((users == null) || (users.size() == 0)) {
			return new RetMsg("error", "����������");
		}
		String pInfo = this.pubReservationInfoDao.selectInfoByUserId(((PubUserVo) users.get(0)).getUserId());
		if (infoDesc.equals(pInfo)) {
			String srecurityCode = SecurityCodeUtil.setCode();
			RetMsg retMsg = new RetMsg("success", true, "��������");
			Map<String, String> m = new HashMap<String, String>();
			m.put("srecurityCode", srecurityCode);
			retMsg.setBean(m);

			return retMsg;
		}
		return new RetMsg("error", "������������");
	}

	public RetMsg updatePubReservationInfo(PubReservationInfo pInfo) {
		int retflag = 1;
		String saveOrUpdateTip = "";
		if (StringUtil.trimEmpty(pInfo.getInfoId())) {
			pInfo.setInfoId(SystemUtil.getUUID());
			pInfo.setUserId(CommonDataUtil.getUserId());

			pInfo.setCreateTime(SysDateUtil.getSystemMillTime());
			retflag = this.pubReservationInfoDao.insert(pInfo);
			saveOrUpdateTip = "����������������";
		} else {
			pInfo.setUserId(CommonDataUtil.getUserId());
			pInfo.setCreateTime(SysDateUtil.getSystemDateTime());
			retflag = this.pubReservationInfoDao.updateByPrimaryKey(pInfo);
			saveOrUpdateTip = "����������������";
		}
		if (retflag == 1) {
			return new RetMsg("success", saveOrUpdateTip);
		}
		return new RetMsg("error", "����������������");
	}

	public PubReservationInfo selectPubReservationInfo() {
		PubReservationInfo pInfo = new PubReservationInfo();
		pInfo.setUserId(CommonDataUtil.getCurUser().getUserId());
		return (PubReservationInfo) this.pubReservationInfoDao.selectOne(pInfo);
	}
}
